package com.songcha.module_main.ui.fragment.calendar

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_base.mvvm.base.BaseViewModel
import com.songcha.library_common.util.DateUtil
import com.songcha.library_common.util.TimeUtil
import com.songcha.library_network.bean.BaseBean
import com.songcha.module_main.bean.calendar_task.CalendarTaskRecordBean
import com.songcha.module_memo.bean.wooden_fish.WoodenFishRecordBean
import kotlinx.coroutines.flow.MutableStateFlow
import java.util.Calendar
import java.util.Date

class CalendarViewModel(app:Application):BaseViewModel<CalendarRepository>(app) {
    var currentDate = MutableLiveData(intArrayOf(2024,1,1))
    var today = intArrayOf(2024,1,1)
    var todayGongDe = 0
    val curTaskRecordBean = MutableStateFlow(CalendarTaskRecordBean.DataBean())
    var todayTaskRecordBean = CalendarTaskRecordBean.DataBean()


    init {
        setTodayDate()
    }

    fun setTodayDate(){
        val date = Date(TimeUtil.getCurrentTime())
        val year = DateUtil.getYear(date)
        val month = DateUtil.getMonth(date)
        val day = DateUtil.getDay(date)
        today[0] = year
        today[1] = month
        today[2] = day
    }

    fun setCurrentDate(year:Int,month:Int,day:Int){
        val arr = intArrayOf(year,month,day)
        currentDate.value = arr
    }

    fun currentDateIsToday():Boolean{
        return currentDate.value!![0] == today[0] && currentDate.value!![1] == today[1] && currentDate.value!![2] == today[2]
    }

    fun diffCurrentDateToday():Int{
        return DateUtil.diffCalendarDay(DateUtil.yearMonthDay2Calendar(currentDate.value!![0],currentDate.value!![1],currentDate.value!![2]),
            DateUtil.yearMonthDay2Calendar(today[0],today[1],today[2]))
    }

    private fun currentDate2TimeStr():String{
        val time = DateUtil.yearMonthDay2Calendar(currentDate.value!![0],currentDate.value!![1],currentDate.value!![2]).timeInMillis
        val str = TimeUtil.formatTime(time,"yyyy-MM-dd HH:mm")
        return str
    }

    private fun dateStr2Calendar(dateStr:String):Calendar{
        return DateUtil.timeStamp2Calendar(TimeUtil.dateString2Stamp(dateStr))
    }

    fun getTodayGongDe(block:()->Unit){
        handleApiDataObserver(repository.getWoodenFishRecord(),object:IApiResult<WoodenFishRecordBean>{
            override fun onSuccess(bean: WoodenFishRecordBean) {
                todayGongDe = bean.data.todayMeritsVirtues
                block()
            }

        })
    }

    fun updateCalendarTaskRecord(type:Int){
        val bean = todayTaskRecordBean
        when(type){
            2->{
                bean.two = 1
            }
            3->{
                bean.three = 1
            }
            4->{
                bean.four = 1
            }
        }
        handleApiDataObserver(repository.updateCalendarTaskRecord(bean),object:IApiResult<BaseBean>{
            override fun onSuccess(bean: BaseBean) {
                getCalendarTaskRecord()
            }

        })
    }

    fun getCalendarTaskRecord(){
        handleApiDataObserver(repository.getCalendarTaskRecord(currentDate2TimeStr()),object:IApiResult<CalendarTaskRecordBean>{
            override fun onSuccess(bean: CalendarTaskRecordBean) {
                if(bean.data != null){
                    curTaskRecordBean.value = bean.data

                    if(currentDateIsToday()){
                        curTaskRecordBean.value = bean.data
                        if(bean.data.one == 0)
                            curTaskRecordBean.value.one = todayGongDe
                        //deep clone
                        todayTaskRecordBean = CalendarTaskRecordBean.DataBean().apply{
                            id = bean.data.id
                            one = if(bean.data.one > 0) bean.data.one else todayGongDe
                            two = bean.data.two
                            three = bean.data.three
                            four = bean.data.four
                            createDate = bean.data.createDate
                        }

                    }else{
                        curTaskRecordBean.value = bean.data
                    }
                }else{
                    val trb = CalendarTaskRecordBean.DataBean()
                    trb.createDate = currentDate2TimeStr()
                    curTaskRecordBean.value = trb
                    if(currentDateIsToday()){
                        trb.one = todayGongDe
                        todayTaskRecordBean = trb
                    }

                }

            }

            override fun onError(e: Throwable) {

            }

        })
    }


}